import * as Cesium from 'cesium'

export class DynamicWallMaterialProperty {
    constructor(options) {
        // 默认参数设置
        this._definitionChanged = new Cesium.Event();
        this._color = undefined;
        this._colorSubscription = undefined;
        this.color = options.color;
        this.duration = options.duration;
        this.trailImage = options.trailImage;
        this._time = (new Date()).getTime();
    }
    // 材质类型
    getType(time) {
        return "DynamicWall";
    }
    // 这个方法在每次渲染时被调用，result的参数会传入glsl中。
    getValue(time, result) {
        if (!Cesium.defined(result)) {
            result = {};
        }
        result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
        if (this.trailImage) {
            result.image = this.trailImage;
        } else {
            result.image = 'src/assets/images/cesium/wallPic-blue.png'
        }
        if (this.duration) {
            result.time = (((new Date()).getTime() - this._time) % this.duration) / this.duration;
        }
        return result;
    }
    equals(other) {
        return this === other ||
            (other instanceof DynamicWallMaterialProperty &&
                Cesium.Property.equals(this._color, other._color))
    }
}

Object.defineProperties(DynamicWallMaterialProperty.prototype, {
    isConstant: {
        get: function () {
            return false;
        }
    },
    definitionChanged: {
        get: function () {
            return this._definitionChanged;
        }
    },
    color: Cesium.createPropertyDescriptor('color')
});

function _getDirectionWallShader(options) {
    var materail = "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
      {\n\
          czm_material material = czm_getDefaultMaterial(materialInput);\n\
          vec2 st = materialInput.st;";
    if (options.freely == "vertical") { //（由下到上）
        materail += "vec4 colorImage = texture(image, vec2(fract(st.s), fract(float(" + options.count + ")*st.t" + options.direction + " time)));\n\ ";
    } else { //（逆时针）
        materail += "vec4 colorImage = texture(image, vec2(fract(float(" + options.count + ")*st.s " + options.direction + " time), fract(st.t)));\n\ ";
    }
    //泛光
    materail += "vec4 fragColor;\n\
          fragColor.rgb = (colorImage.rgb+color.rgb) / 1.0;\n\
          fragColor = czm_gammaCorrect(fragColor);\n\
          material.diffuse = colorImage.rgb;\n\
          material.alpha = colorImage.a;\n\
          material.emission = fragColor.rgb;\n\
          return material;\n\
      }";
    return materail
}

Cesium.Material._materialCache.addMaterial("DynamicWall", {
    fabric: {
        type: "DynamicWall",
        uniforms: {
            // uniforms参数跟我们上面定义的参数以及getValue方法中返回的result对应，这里值是默认值
            color: new Cesium.Color(1, 1, 1, 0),
            image: '@/assets/images/cesium/wallPic-blue.png',
            time: 3000
        },
        // source编写glsl，可以使用uniforms参数，值来自getValue方法的result
        source: _getDirectionWallShader({
            count: 4.0,
            freely: 'vertical',
            direction: '-'
        })
    },
    translucent: function (material) {
        return true;
    }
});
